'use strict';

define([
    'angular',
    'app',
    'dependencyResolver'
], function (angular, app, dependencyResolver) {
    var routeConfig = {
        routes: {
            // Home模块
            '/login': {
                templateUrl: 'app/home/view/login.html',
                controller: 'LoginController',
                dependencies: ['home/controller/loginController']
            },
            '/home': {
                templateUrl: 'app/home/view/home.html',
                controller: 'HomeController',
                dependencies: ['home/controller/homeController']
            },

            // Solr模块
            '/solr/config': {
                templateUrl: 'app/solr/view/config.html',
                controller: 'ConfigController',
                dependencies: ['solr/controller/configController']
            },
            '/solr/backup': {
                templateUrl: 'app/solr/view/backup.html',
                controller: 'BackupController',
                dependencies: ['solr/controller/backupController']
            },
            '/solr/collection': {
                templateUrl: 'app/solr/view/collection.html',
                controller: 'CollectionController',
                dependencies: ['solr/controller/collectionController']
            },
            '/solr/node': {
                templateUrl: 'app/solr/view/node.html',
                controller: 'NodeController',
                dependencies: ['solr/controller/nodeController']
            },
            '/solr/replica': {
                templateUrl: 'app/solr/view/replica.html',
                controller: 'ReplicaController',
                dependencies: ['solr/controller/replicaController']
            },

            // Collection模块
            '/collection/:id/overview': {
                templateUrl: 'app/collection/view/overview.html',
                controller: 'OverviewController',
                dependencies: ['collection/controller/overviewController']
            },
            '/collection/:id/query': {
                templateUrl: 'app/collection/view/query.html',
                controller: 'QueryController',
                dependencies: ['collection/controller/queryController']
            },
            '/collection/:id/update': {
                templateUrl: 'app/collection/view/update.html',
                controller: 'UpdateController',
                dependencies: ['collection/controller/updateController']
            },
            '/collection/:id/delete': {
                templateUrl: 'app/collection/view/delete.html',
                controller: 'DeleteController',
                dependencies: ['collection/controller/deleteController']
            },
            '/collection/:id/analysis': {
                templateUrl: 'app/collection/view/analysis.html',
                controller: 'AnalysisController',
                dependencies: ['collection/controller/analysisController']
            },
            '/collection/:id/file': {
                templateUrl: 'app/collection/view/file.html',
                controller: 'FileController',
                dependencies: ['collection/controller/fileController']
            },

            // Client模块
            '/client/expression': {
                templateUrl: 'app/client/view/expression.html',
                controller: 'ExpressionController',
                dependencies: ['client/controller/expressionController']
            },
            '/client/filterword': {
                templateUrl: 'app/client/view/filterword.html',
                controller: 'FilterWordController',
                dependencies: ['client/controller/filterwordController']
            },

            // 统计模块
            '/stat/visit': {
                templateUrl: 'app/stat/view/visit.html',
                controller: 'VisitController',
                dependencies: ['stat/controller/visitController']
            },
            '/stat/qps': {
                templateUrl: 'app/stat/view/qps.html',
                controller: 'QpsController',
                dependencies: ['stat/controller/qpsController']
            },
            '/stat/grow': {
                templateUrl: 'app/stat/view/grow.html',
                controller: 'GrowController',
                dependencies: ['stat/controller/growController']
            },
            '/stat/status': {
                templateUrl: 'app/stat/view/status.html',
                controller: 'StatusController',
                dependencies: ['stat/controller/statusController']
            },
            '/stat/load': {
                templateUrl: 'app/stat/view/load.html',
                controller: 'LoadController',
                dependencies: ['stat/controller/loadController']
            },
            '/stat/cache': {
                templateUrl: 'app/stat/view/cache.html',
                controller: 'CacheController',
                dependencies: ['stat/controller/cacheController']
            },
            '/stat/memusage': {
                templateUrl: 'app/stat/view/memusage.html',
                controller: 'MemusageController',
                dependencies: ['stat/controller/memusageController']
            },

            // Logger模块
            '/log/slow': {
                templateUrl: 'app/log/view/slow.html',
                controller: 'SlowController',
                dependencies: ['log/controller/slowController']
            },
            '/log/error': {
                templateUrl: 'app/log/view/error.html',
                controller: 'ErrorController',
                dependencies: ['log/controller/errorController']
            },
            '/log/operation': {
                templateUrl: 'app/log/view/operation.html',
                controller: 'OperationController',
                dependencies: ['log/controller/operationController']
            },
            '/log/scheduler': {
                templateUrl: 'app/log/view/operation.html',
                controller: 'OperationController',
                dependencies: ['log/controller/operationController']
            },

            // System模块
            '/system/admin': {
                templateUrl: 'app/system/view/admin.html',
                controller: 'AdminController',
                dependencies: ['system/controller/adminController']
            },
            '/system/role': {
                templateUrl: 'app/system/view/role.html',
                controller: 'RoleController',
                dependencies: ['system/controller/roleController']
            },
            '/system/dict': {
                templateUrl: 'app/system/view/dict.html',
                controller: 'DictController',
                dependencies: ['system/controller/dictController']
            },
            '/system/scheduler': {
                templateUrl: 'app/system/view/scheduler.html',
                controller: 'SchedulerController',
                dependencies: ['system/controller/schedulerController']
            },
            '/system/sql': {
                templateUrl: 'app/system/view/sql.html',
                controller: 'SqlController',
                dependencies: ['system/controller/sqlController']
            }
        }
    };
    return app.config(['$routeProvider', function ($routeProvider) {
        angular.forEach(routeConfig.routes, function(route, path) {
            $routeProvider.when(path, {
                templateUrl: route.templateUrl,
                controller: route.controller,
                resolve: dependencyResolver(route.dependencies)
            });
        });
        var injector = angular.injector(['app.common.services']);
        injector.invoke(['MenuService', function (menuService) {
            $routeProvider.otherwise({redirectTo: function () {
                if (menuService.getMenus().length == 0) {
                    return '/login';
                }
                return menuService.getDefaultMenuUrl();
            }});
        }]);
    }]);
});